using System;
using System.Data;
using System.Data.SqlClient;
using System.Collections;
using System.Configuration;

namespace ProcessesSMS
{
	/// <summary>
	/// Summary description for.
	/// Project name: SMS_xoso
	/// channel: n/a
	/// command code: HN X Y
	/// created: 01/09/09 .
	/// Writer : Nampt + Tuanbc
	/// Company: VIT TELECOM
	/// </summary>

	public class XOSO_SOI
	{
		#region Declare
		private SMS_MO WMO;
		private SMS_MT WMT;
		ArrayList arr = new ArrayList();
		public XOSO_SOI(SMS_MO MO)
		{
			WMO = MO;
		}

		public XOSO_SOI()
		{
		}
		#endregion
		public void run()
		{
			WMT = new SMS_MT(WMO);
			string tmpMsg = WMO.Message.Substring(WMO.CommandCode.Length).Trim().Replace("  ", " ").Replace("  ", " ");
			tmpMsg = tmpMsg.Replace("  ", " ").Trim().ToUpper();
			string[] msg = tmpMsg.Split(new char[] { ' ' });

			string strReturnMsg = "";   
			string tempvalue="";
			strReturnMsg = GetReturnMsg(msg);
			tempvalue= strReturnMsg;
			if(strReturnMsg.Length>160)
			{
				if (strReturnMsg.Length>320)
				{
					// 4 mess
					strReturnMsg = tempvalue.Substring(0,160);
					WMT.AddMessage(WMO.UserID, strReturnMsg, SMS_ContentType.SMS_TEXT);
					WMT.MsgType = SMS_MsgType.SMS_TINHTIEN;
					
					strReturnMsg = tempvalue.Substring(160,160);
					WMT.AddMessage(WMO.UserID, strReturnMsg, SMS_ContentType.SMS_TEXT);
					WMT.MsgType = SMS_MsgType.SMS_TINHTIEN;

					strReturnMsg = tempvalue.Substring(320);
					WMT.AddMessage(WMO.UserID, strReturnMsg, SMS_ContentType.SMS_TEXT);
					WMT.MsgType = SMS_MsgType.SMS_TINHTIEN;

					WMT.Insert(); 
				}
				else
				{
					// 3 mess
					strReturnMsg = tempvalue.Substring(160);
					WMT.AddMessage(WMO.UserID, strReturnMsg, SMS_ContentType.SMS_TEXT);
					WMT.MsgType = SMS_MsgType.SMS_TINHTIEN;

					strReturnMsg = tempvalue.Substring(0,160);
					WMT.AddMessage(WMO.UserID, strReturnMsg, SMS_ContentType.SMS_TEXT);
					WMT.MsgType = SMS_MsgType.SMS_TINHTIEN;									

					WMT.Insert(); 
				}
			}
			else
			{
				// 2 mess
				WMT.AddMessage(WMO.UserID, strReturnMsg, SMS_ContentType.SMS_TEXT);
				WMT.MsgType = SMS_MsgType.SMS_TINHTIEN;
				WMT.Insert(); 
			}
			return;         
		}
		public string GetReturnMsg(string[] arr)
		{			
			string msgBody="";
			string strSQLDD;
			string strSQL;
			string connStr = ConfigurationSettings.AppSettings["STOCK_CONNECTION_STRING"].ToString();
			SqlConnection conn = new SqlConnection(connStr);
			if (arr[0]=="TD" || arr[0]=="HN" || arr[0]=="QN" || arr[0]=="BN" || arr[0]=="HP" || arr[0]=="ND" || arr[0]=="TB" || arr[0]=="")
			{
				strSQLDD = "select top 7 giaidacbiet, ngay from xoso where mamien='mb' order by ngay desc";
				strSQL ="SELECT TOP 1 ManySpecAward, LessSpecAward, GuessSpecAward FROM XOSO_DD WHERE Mamien='MB'";
			}	
			else
			{
				strSQLDD = "select top 7 giaidacbiet, ngay from xoso where matinh='" + arr[0] + "' order by ngay desc";
				strSQL ="SELECT Top 1 ManySpecAward, LessSpecAward, GuessSpecAward FROM XOSO_DD WHERE Matinh='" + arr[0]+ "'";
			}
			if (WMO.ServiceID.Replace("04","")=="6572" || WMO.ServiceID.Replace("04","")=="6772" || (WMO.UserID=="84984696925"))
			{
				conn.Open();
				SqlDataAdapter oda = new SqlDataAdapter(strSQLDD,conn);
				DataSet ods = new DataSet();
				oda.Fill(ods);
				DataTable odt = ods.Tables[0];
				conn.Close();
				for (int i=0; i<odt.Rows.Count; i++)
				{
					DateTime datetime = Convert.ToDateTime(odt.Rows[i]["Ngay"]);
					msgBody += datetime.Day + "/" + datetime.Month + ": " + odt.Rows[i]["GiaiDacBiet"].ToString() + "\n";
				}
				msgBody = "SOI SO DAI LOC\n" + "GDB CUA 7 KET QUA GAN NHAT\n" + msgBody.Trim().Replace(" ","");
				WMT.AddMessage(WMO.UserID, msgBody, SMS_ContentType.SMS_TEXT);
				WMT.MsgType = SMS_MsgType.SMS_TINHTIEN;

				string temp="";
				conn.Open();
				SqlDataAdapter da = new SqlDataAdapter(strSQL, conn);
				DataSet ds = new DataSet();
				da.Fill(ds);
				DataTable dt = ds.Tables[0];
				conn.Close();
				temp = "Nhieu: " + dt.Rows[0]["ManySpecAward"].ToString() +"\nIt: " + dt.Rows[0]["LessSpecAward"].ToString()+ "\nDu Doan GDB: "+ dt.Rows[0]["GuessSpecAward"].ToString() + "\n";
				msgBody = "SOI SO DAI LOC 2:\n" + "GDB VE NHIEU NHAT VA IT NHAT\n" + temp;
				msgBody = msgBody.Replace("lần" , "lan");
			}
			else if (WMO.ServiceID.Replace("04","")=="6372" || WMO.ServiceID.Replace("04","")=="6272")
			{
				conn.Open();
				SqlDataAdapter oda = new SqlDataAdapter(strSQLDD,conn);
				DataSet ods = new DataSet();
				oda.Fill(ods);
				DataTable odt = ods.Tables[0];
				conn.Close();
				for (int i=0; i<odt.Rows.Count; i++)
				{
					DateTime datetime = Convert.ToDateTime(odt.Rows[i]["Ngay"]);
					msgBody += datetime.Day + "/" + datetime.Month + ": " + odt.Rows[i]["GiaiDacBiet"].ToString() + "\n";
				}
				msgBody = "SOI SO DAI LOC\n" + "GDB CUA 7 KET QUA GAN NHAT\n" + msgBody.Trim().Replace(" ","");
			}
			else
			{
				msgBody = "De SOI SO DAI LOC cung 6x72 soan SOI Matinh gui 6772. Vi du: De SOI SO DAI LOC kqxs mien bac soan SOI TD gui 6772. Chuc cac ban may man !";
			}
			return msgBody;
		}
	}		
}
